package com.yatong.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yatong.commons.util.PageData;
import com.yatong.entity.Speech;
import com.yatong.service.SpeechService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (Speech)表控制层
 *
 * @author makejava
 * @since 2018-11-06 19:01:50
 */
@Controller
@RequestMapping("speech")
public class SpeechController {
    /**
     * 服务对象
     */
    @Autowired
    private SpeechService speechService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Speech selectOne(Integer id) {
        return this.speechService.queryById(id);
    }


    /**
     * 访问话术首页
     * @param model
     * @return
     */
    @RequestMapping("/")
    public String speech_index(Model model){
        return "speech/speech_list";
    }


    /**
     * 通过查询条件查询多条数据
     *
     * @param speech
     * @return
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public Object queryAll(Speech speech,@RequestParam Map<String, String> params){

        PageHelper.startPage(Integer.parseInt(params.get("pageNumber")), Integer.parseInt(params.get("pageSize")));
        List<Speech> speeches = this.speechService.queryAll(speech);

        PageInfo<Speech> page = new PageInfo<Speech>(speeches);
        return new PageData().ResultMap(page);
    }

    /**
     * 访问新增话术页
     * @param model
     * @return
     */
    @RequestMapping("/speech_add")
    public String speech_add(Model model){
        return "speech/speech_add";
    }

    /**
     * 保存新增公司
     * @param model
     * @return
     */
    @RequestMapping("/speech_save")
    @ResponseBody
    public String speech_save(Speech speech,Model model){
        String flag = "0";
        Speech insert = this.speechService.insert(speech);
        if (insert != null)
            flag = "1";
        return flag;
    }

    /**
     * 访问编辑话术信息页
     * @param model
     * @return
     */
    @RequestMapping("/speech_edit")
    public String speech_edit(@RequestParam(required = false) String id,Model model){
        Speech speech = this.speechService.queryById(Integer.parseInt(id));
        model.addAttribute("speech",speech);
        return "speech/speech_edit";
    }


    /**
     * 修改话术信息
     * @param model
     * @return
     */
    @RequestMapping("/speech_update")
    @ResponseBody
    public int speech_update(Speech speech,Model model){
        int flag = 0;

        Speech thisSpeech = this.speechService.queryById(speech.getId());
        thisSpeech.setSpeechName(speech.getSpeechName());
        thisSpeech.setSpeechContent(speech.getSpeechContent());

        flag = speechService.update(thisSpeech);
        return flag;
    }
}